#include "dev.h"

#include "gtc_client_pal.h"
#include "data.h"
#include "mod_led.h"
#include "mod_rfcard.h"
#include "mod_port.h"
#include "mod_emeter.h"
#include "mod_gun.h"
#include "alarm.h"
#include "log.h"
#include "utility.h"
#include "guictrl.h"
#include "errcode.h"
#include "record.h"
#include "platform.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>



enum ACK_STATE{
    ACK_STATE_SUCCESS = 0,
    ACK_STATE_FAIL,
    ACK_STATE_START_TIMEOUT, //启动超时
};

extern struct ClientUserGunInfo gun_status[];
static struct ClientUserBmsInfo bms_status[DEV_CHARGE_GUN_CNT]={0};



struct  ClientUserBmsInfo *dev_get_user_client_bms_data(void){
    return &bms_status[0];
}

#if 0  //状态机
#endif

void client_sync_meter_kwh_set(int32_t index, uint32_t kwh);



//设备
int32_t dev_isswipcard(char_t *cardid){
	return rfcard_read(cardid);
}

int32_t dev_setsystime(void *p){
    T_TimeFmt *t = (T_TimeFmt *)p;
    rtc_set_time(t->year, t->month, t->date, t->hour, t->min, t->sec,1);

    return 0;
}

int dev_get_dev_mode(void){
    return DEV_CHARGE_MODE_AC;
}

int32_t dev_init_init(uint8_t gun_id){
    
    return 0;
}

int32_t dev_is_init_succ(uint8_t gun_id){
    return RET_TRUE;
}

int32_t dev_idle_start(uint8_t gun_id){
    return 0;
}

uint8_t dev_get_gun_link(uint8_t gun_id){
    uint8_t state = gun_get_state(gun_id);
    if (state == DRV_GUN_STATE_INCAR || state == DRV_GUN_STATE_READY){
        return RET_TRUE;
    }
    return RET_FALSE;
}
uint8_t dev_get_relay_state(uint8_t gun_id)
{
	 return gun_get_relay_state(gun_id);
}
int32_t dev_is_start_charging_finish(uint8_t gun_id, char_t *reasion){
    uint8_t state = gun_get_state(gun_id);
    if (state == DRV_GUN_STATE_READY){
        gun_start_charge(gun_id);
        return RET_TRUE;
    }
    memcpy(reasion,"100D", 4);
    return RET_FALSE;
}


int32_t dev_is_stop_charge(uint8_t gun_id, char_t *reasion){
    uint8_t state = gun_get_state(gun_id);
    if (state != DRV_GUN_STATE_READY){
        return RET_TRUE;
    }
    return RET_FALSE;
}

int32_t dev_is_stop_charge_finish(uint8_t gun_id){     
    return RET_TRUE;
}

int32_t dev_is_low_current(uint8_t gun_id){
	uint32_t current = dev_get_current(gun_id);
  //printf("current:%d\r\n",current);
  if (current < 100){//小于100ma 停止充电
      return RET_TRUE;
  }
  return RET_FALSE;
}

int32_t dev_start_charge_fail(uint8_t gun_id, char_t *reasion){
    gun_stop_charge(gun_id);
    return 0;
}

int32_t dev_stop_charge(uint8_t gun_id, char_t *reasion){
    gun_stop_charge(gun_id);
    return 0;
}



void controler_keeplive(uint8_t gun_id){

}

void controler_set_time(void){

}

int32_t controler_init(void){
    return 0;
}


uint32_t controler_get_vol(uint8_t gun_id){
    gun_status[gun_id-1].dc_charge_voltage = emeter_getvalue(gun_id, EM_TYPE_VOL);
    return gun_status[gun_id-1].dc_charge_voltage;
}

uint32_t controler_get_current(uint8_t gun_id){
    gun_status[gun_id-1].dc_charge_current = emeter_getvalue(gun_id, EM_TYPE_CURRT)/100;
    return gun_status[gun_id-1].dc_charge_current;
}

uint32_t controler_get_need_vol(uint8_t gun_id){
     return 0;
}
uint32_t controler_get_need_currt(uint8_t gun_id){
     return 0;
}
uint32_t controler_get_left_time(uint8_t gun_id){
     return 0;
}

uint32_t controler_get_soc(uint8_t gun_id) {
     return 0;
}
